package com.mpm.ai.services;


import com.mpm.ai.data.*;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderService {

	private final List<Order> db = new ArrayList<>();

	public OrderService() {
		initDemoData();
	}

	private void initDemoData() {
		// 模拟数据库数据
		db.add(new Order("DD001", LocalDate.now(), "CP001", 200, OrderStatus.CONFIRMED));
		db.add(new Order("DD002", LocalDate.now(), "CP002", 300, OrderStatus.CONFIRMED));
		db.add(new Order("DD003", LocalDate.now(), "CP003",200, OrderStatus.CONFIRMED));
		db.add(new Order("DD004", LocalDate.now(), "CP001", 100, OrderStatus.CONFIRMED));
		db.add(new Order("DD005", LocalDate.now(), "CP002", 200, OrderStatus.CONFIRMED));
	}

	// 获取所有订单
	public List<Order> getOrders() {
		// 模拟数据库查询数据
		return db;
	}

	// 根据订单号查询订单
	private Order findOrder(String orderNumber) {
		return db
			.stream()
			.filter(b -> b.getOrderNumber().equalsIgnoreCase(orderNumber))
			.findFirst()
			.orElseThrow(() -> new IllegalArgumentException("订单未找到"));
	}

	// 取消订单
	public void cancelOrder(String orderNumber) {
		var order = findOrder(orderNumber);
		order.setOrderStatus(OrderStatus.CANCELLED);
	}

	// 创建订单
	public void createOrder(String goodsCode, Integer num) {
		db.add(new Order("DD"+String.format("%03d", db.size() + 1), LocalDate.now(), goodsCode, num, OrderStatus.CONFIRMED));
	}
}
